package com.stock.demo.function.market.presenter;

import com.lzy.okgo.OkGo;
import com.stock.demo.function.market.view.MarketView;
import com.stock.demo.function.share.view.StockDetailActivity;
import com.stock.demo.model.MarketDetailBean;
import com.stock.demo.model.MarketHotBean;
import com.stock.demo.model.MarketIndexBean;
import com.stock.demo.model.MarketViewBody;
import com.stock.demo.model.MarketViewFoot;
import com.stock.demo.model.MarketViewHead;
import com.stock.demo.model.MarketViewType;
import com.stock.demo.util.StockUtils;
import com.stock.demo.util.ThreadUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;

import okhttp3.Response;

/**
 * Created by XuZhaoyu on 2017/6/26.
 */

public class MarketPresenterImpl implements MarketPresenter {

    private MarketView mMarketView;
    private ArrayList<MarketViewType> mMarketViewTypes;
    private boolean mIsLoading;

    public MarketPresenterImpl(MarketView marketView) {
        mMarketView = marketView;
    }

    @Override
    public void getMarketInfo() {
        ThreadUtils.runOnSubThread(new Runnable() {
            @Override
            public void run() {
                if (mIsLoading) {
                    return;
                }
                mIsLoading = true;
                mMarketViewTypes = new ArrayList<>();

                //添加大盘指数栏目
                MarketIndexBean stockIndexSh = getStockIndex(StockUtils.getStockInfoUrl("000001"));//上证指数
                MarketIndexBean stockIndexSz = getStockIndex(StockUtils.getStockInfoUrl("399001"));//深证成数
                MarketIndexBean stockIndexCy = getStockIndex(StockUtils.getStockInfoUrl("399006"));//创业板指
                if (stockIndexSh != null && stockIndexSz != null && stockIndexCy != null) {
                    mMarketViewTypes.add(new MarketViewHead("大盘指数", stockIndexSh, stockIndexSz, stockIndexCy));
                }

                //添加热门行业栏目
                MarketViewBody hotIndustry = new MarketViewBody("热门行业");
                hotIndustry.mMarketHotBeen.add(new MarketHotBean("造纸", "5.00%", "青山纸业"));
                hotIndustry.mMarketHotBeen.add(new MarketHotBean("钢铁", "4.00%", "常宝股份"));
                hotIndustry.mMarketHotBeen.add(new MarketHotBean("银行", "3.00%", "宁波银行"));
                hotIndustry.mMarketHotBeen.add(new MarketHotBean("煤炭", "2.00%", "恒源煤电"));
                hotIndustry.mMarketHotBeen.add(new MarketHotBean("证券", "1.00%", "国元证券"));
                hotIndustry.mMarketHotBeen.add(new MarketHotBean("物流", "0.50%", "华鹏飞"));
                mMarketViewTypes.add(hotIndustry);

                //添加热门概念栏目
                MarketViewBody hotConcept = new MarketViewBody("热门概念");
                hotConcept.mMarketHotBeen.add(new MarketHotBean("基因测序", "5.00%", "荣之联"));
                hotConcept.mMarketHotBeen.add(new MarketHotBean("特种钢材", "4.00%", "常宝股份"));
                hotConcept.mMarketHotBeen.add(new MarketHotBean("两桶油改革", "3.00%", "大庆华科"));
                hotConcept.mMarketHotBeen.add(new MarketHotBean("黄金", "2.00%", "西藏天路"));
                hotConcept.mMarketHotBeen.add(new MarketHotBean("白酒", "1.00%", "维维股份"));
                hotConcept.mMarketHotBeen.add(new MarketHotBean("稀土永磁", "0.50%", "北方稀土"));
                mMarketViewTypes.add(hotConcept);

                //添加涨幅榜栏目
                MarketViewFoot topUp = new MarketViewFoot("涨幅榜");
                MarketDetailBean upStockDetail0 = getStockDetail(StockUtils.getStockInfoUrl("300676"));
                MarketDetailBean upStockDetail1 = getStockDetail(StockUtils.getStockInfoUrl("603789"));
                MarketDetailBean upStockDetail2 = getStockDetail(StockUtils.getStockInfoUrl("603305"));
                MarketDetailBean upStockDetail3 = getStockDetail(StockUtils.getStockInfoUrl("603933"));
                MarketDetailBean upStockDetail4 = getStockDetail(StockUtils.getStockInfoUrl("603757"));
                MarketDetailBean upStockDetail5 = getStockDetail(StockUtils.getStockInfoUrl("300671"));
                MarketDetailBean upStockDetail6 = getStockDetail(StockUtils.getStockInfoUrl("002802"));
                MarketDetailBean upStockDetail7 = getStockDetail(StockUtils.getStockInfoUrl("603595"));
                if (upStockDetail0 != null) {
                    topUp.mMarketFootBeen.add(upStockDetail0);
                }
                if (upStockDetail1 != null) {
                    topUp.mMarketFootBeen.add(upStockDetail1);
                }
                if (upStockDetail2 != null) {
                    topUp.mMarketFootBeen.add(upStockDetail2);
                }
                if (upStockDetail3 != null) {
                    topUp.mMarketFootBeen.add(upStockDetail3);
                }
                if (upStockDetail4 != null) {
                    topUp.mMarketFootBeen.add(upStockDetail4);
                }
                if (upStockDetail5 != null) {
                    topUp.mMarketFootBeen.add(upStockDetail5);
                }
                if (upStockDetail6 != null) {
                    topUp.mMarketFootBeen.add(upStockDetail6);
                }
                if (upStockDetail7 != null) {
                    topUp.mMarketFootBeen.add(upStockDetail7);
                }
                mMarketViewTypes.add(topUp);

                //添加跌幅榜栏目
                MarketViewFoot topDown = new MarketViewFoot("跌幅榜");
                MarketDetailBean downStockDetail0 = getStockDetail(StockUtils.getStockInfoUrl("000806"));
                MarketDetailBean downStockDetail1 = getStockDetail(StockUtils.getStockInfoUrl("300196"));
                MarketDetailBean downStockDetail2 = getStockDetail(StockUtils.getStockInfoUrl("000820"));
                MarketDetailBean downStockDetail3 = getStockDetail(StockUtils.getStockInfoUrl("000887"));
                MarketDetailBean downStockDetail4 = getStockDetail(StockUtils.getStockInfoUrl("300595"));
                MarketDetailBean downStockDetail5 = getStockDetail(StockUtils.getStockInfoUrl("002679"));
                MarketDetailBean downStockDetail6 = getStockDetail(StockUtils.getStockInfoUrl("300613"));
                MarketDetailBean downStockDetail7 = getStockDetail(StockUtils.getStockInfoUrl("300469"));
                if (downStockDetail0 != null) {
                    topDown.mMarketFootBeen.add(downStockDetail0);
                }
                if (downStockDetail1 != null) {
                    topDown.mMarketFootBeen.add(downStockDetail1);
                }
                if (downStockDetail2 != null) {
                    topDown.mMarketFootBeen.add(downStockDetail2);
                }
                if (downStockDetail3 != null) {
                    topDown.mMarketFootBeen.add(downStockDetail3);
                }
                if (downStockDetail4 != null) {
                    topDown.mMarketFootBeen.add(downStockDetail4);
                }
                if (downStockDetail5 != null) {
                    topDown.mMarketFootBeen.add(downStockDetail5);
                }
                if (downStockDetail6 != null) {
                    topDown.mMarketFootBeen.add(downStockDetail6);
                }
                if (downStockDetail7 != null) {
                    topDown.mMarketFootBeen.add(downStockDetail7);
                }
                mMarketViewTypes.add(topDown);

                //添加振幅榜栏目
                MarketViewFoot topAmp = new MarketViewFoot("振幅榜");
                MarketDetailBean ampStockDetail0 = getStockDetail(StockUtils.getStockInfoUrl("603029"));
                MarketDetailBean ampStockDetail1 = getStockDetail(StockUtils.getStockInfoUrl("300515"));
                MarketDetailBean ampStockDetail2 = getStockDetail(StockUtils.getStockInfoUrl("600175"));
                MarketDetailBean ampStockDetail3 = getStockDetail(StockUtils.getStockInfoUrl("300259"));
                MarketDetailBean ampStockDetail4 = getStockDetail(StockUtils.getStockInfoUrl("002072"));
                MarketDetailBean ampStockDetail5 = getStockDetail(StockUtils.getStockInfoUrl("000893"));
                MarketDetailBean ampStockDetail6 = getStockDetail(StockUtils.getStockInfoUrl("002827"));
                MarketDetailBean ampStockDetail7 = getStockDetail(StockUtils.getStockInfoUrl("603988"));
                if (ampStockDetail0 != null) {
                    topAmp.mMarketFootBeen.add(ampStockDetail0);
                }
                if (ampStockDetail1 != null) {
                    topAmp.mMarketFootBeen.add(ampStockDetail1);
                }
                if (ampStockDetail2 != null) {
                    topAmp.mMarketFootBeen.add(ampStockDetail2);
                }
                if (ampStockDetail3 != null) {
                    topAmp.mMarketFootBeen.add(ampStockDetail3);
                }
                if (ampStockDetail4 != null) {
                    topAmp.mMarketFootBeen.add(ampStockDetail4);
                }
                if (ampStockDetail5 != null) {
                    topAmp.mMarketFootBeen.add(ampStockDetail5);
                }
                if (ampStockDetail6 != null) {
                    topAmp.mMarketFootBeen.add(ampStockDetail6);
                }
                if (ampStockDetail7 != null) {
                    topAmp.mMarketFootBeen.add(ampStockDetail7);
                }
                mMarketViewTypes.add(topAmp);

                //添加换手率榜栏目
                MarketViewFoot topTur = new MarketViewFoot("换手率");
                MarketDetailBean turStockDetail0 = getStockDetail(StockUtils.getStockInfoUrl("603617"));
                MarketDetailBean turStockDetail1 = getStockDetail(StockUtils.getStockInfoUrl("603933"));
                MarketDetailBean turStockDetail2 = getStockDetail(StockUtils.getStockInfoUrl("603331"));
                MarketDetailBean turStockDetail3 = getStockDetail(StockUtils.getStockInfoUrl("300667"));
                MarketDetailBean turStockDetail4 = getStockDetail(StockUtils.getStockInfoUrl("002879"));
                MarketDetailBean turStockDetail5 = getStockDetail(StockUtils.getStockInfoUrl("002885"));
                MarketDetailBean turStockDetail6 = getStockDetail(StockUtils.getStockInfoUrl("002827"));
                MarketDetailBean turStockDetail7 = getStockDetail(StockUtils.getStockInfoUrl("300668"));
                if (turStockDetail0 != null) {
                    topTur.mMarketFootBeen.add(turStockDetail0);
                }
                if (turStockDetail1 != null) {
                    topTur.mMarketFootBeen.add(turStockDetail1);
                }
                if (turStockDetail2 != null) {
                    topTur.mMarketFootBeen.add(turStockDetail2);
                }
                if (turStockDetail3 != null) {
                    topTur.mMarketFootBeen.add(turStockDetail3);
                }
                if (turStockDetail4 != null) {
                    topTur.mMarketFootBeen.add(turStockDetail4);
                }
                if (turStockDetail5 != null) {
                    topTur.mMarketFootBeen.add(turStockDetail5);
                }
                if (turStockDetail6 != null) {
                    topTur.mMarketFootBeen.add(turStockDetail6);
                }
                if (turStockDetail7 != null) {
                    topTur.mMarketFootBeen.add(turStockDetail7);
                }
                mMarketViewTypes.add(topTur);

                ThreadUtils.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {
                        mIsLoading = false;
                        mMarketView.onGetMarketInfo(mMarketViewTypes);
                    }
                });
            }
        });
    }

    private MarketIndexBean getStockIndex(String indexUrl) {
        try {
            Response response = OkGo.get(indexUrl).execute();
            return getIndexInfo(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public MarketDetailBean getStockDetail(String stockUrl) {
        try {
            Response response = OkGo.get(stockUrl).execute();
            final MarketDetailBean detailBean = getStockInfo(response.body().string());
            if (mMarketView instanceof StockDetailActivity) {
                ThreadUtils.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {
                        mMarketView.onGetStockDetail(detailBean);
                    }
                });
            }
            return detailBean;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //0 中国交建,股票名
    //1 16.700,今日开盘价
    //2 16.700,昨日收盘价
    //3 16.980,当前价格
    //4 17.100,今日最高价
    //5 16.700,今日最低价
    //6 16.980,竞买价-买1
    //7 16.990,竞卖价-卖1
    //8 26723526,成交股数，一般除以100
    //9 453290716.000,成交金额，元为单位，一般以万元为金融单位
    //10 6500,买1申请数
    //11 16.980,买1报价
    //12 97200,买2申请数
    //13 16.970,买2报价
    //14 34900,买3
    //15 16.960,买3
    //16 124400,买4
    //17 16.950,买4
    //18 5800,买5
    //19 16.940,买5
    //20 200,卖1
    //21 16.990,卖1
    //22 526906,卖2
    //23 17.000,卖2
    //24 338900,卖3
    //25 17.010,卖3
    //26 94400,卖4
    //27 17.020,卖4
    //28 94000,卖5
    //29 17.030,卖5
    //30 2017-07-14,日期
    //31 15:00:00,时间
    //32 00

    /**
     * 解析个股
     */
    private MarketDetailBean getStockInfo(String s) {
        String[] split = s.split("=");
        if (split.length == 2) {
            MarketDetailBean marketDetailBean = new MarketDetailBean();
            marketDetailBean.mSymbol = split[0].substring(split[0].length() - 6, split[0].length());
            String[] contents = split[1].substring(split[1].indexOf("\"") + 1, split[1].lastIndexOf("\"")).split(",");
            if (contents.length == 33) {
                marketDetailBean.mName = contents[0];
                double change = Double.valueOf(contents[3]) - Double.valueOf(contents[2]);
                marketDetailBean.mChange = String.valueOf(new BigDecimal(change).setScale(2, RoundingMode.UP));
                double changePercent = change / Double.valueOf(contents[2]) * 100;
                marketDetailBean.mChangePercent = String.valueOf(new BigDecimal(changePercent).setScale(2, RoundingMode.UP)) + "%";
                marketDetailBean.mCurrentPrice = String.valueOf(new BigDecimal(contents[3]).setScale(2, RoundingMode.UP));
                marketDetailBean.mOpenPrice = String.valueOf(new BigDecimal(contents[1]).setScale(2, RoundingMode.UP));
                marketDetailBean.mClosePrice = String.valueOf(new BigDecimal(contents[2]).setScale(2, RoundingMode.UP));
                marketDetailBean.mTopPrice = String.valueOf(new BigDecimal(contents[4]).setScale(2, RoundingMode.UP));
                marketDetailBean.mBottomPrice = String.valueOf(new BigDecimal(contents[5]).setScale(2, RoundingMode.UP));
                Double tur = Double.valueOf(contents[8]) / 100d;
                marketDetailBean.mTurnover = String.valueOf(new BigDecimal(tur).setScale(0, RoundingMode.UP));
                Double vol = Double.valueOf(contents[9]) / 10000d;
                marketDetailBean.mVolume = String.valueOf(new BigDecimal(vol).setScale(2, RoundingMode.UP));

                marketDetailBean.mBuyPrice1 = String.valueOf(new BigDecimal(contents[11]).setScale(2, RoundingMode.UP));
                Double buyCount1 = Double.valueOf(contents[10]) / 10000d;
                marketDetailBean.mBuyCount1 = String.valueOf(new BigDecimal(buyCount1).setScale(1, RoundingMode.UP)) + "万";

                marketDetailBean.mBuyPrice2 = String.valueOf(new BigDecimal(contents[13]).setScale(2, RoundingMode.UP));
                Double buyCount2 = Double.valueOf(contents[12]) / 10000d;
                marketDetailBean.mBuyCount2 = String.valueOf(new BigDecimal(buyCount2).setScale(1, RoundingMode.UP)) + "万";

                marketDetailBean.mBuyPrice3 = String.valueOf(new BigDecimal(contents[15]).setScale(2, RoundingMode.UP));
                Double buyCount3 = Double.valueOf(contents[14]) / 10000d;
                marketDetailBean.mBuyCount3 = String.valueOf(new BigDecimal(buyCount3).setScale(1, RoundingMode.UP)) + "万";

                marketDetailBean.mBuyPrice4 = String.valueOf(new BigDecimal(contents[17]).setScale(2, RoundingMode.UP));
                Double buyCount4 = Double.valueOf(contents[16]) / 10000d;
                marketDetailBean.mBuyCount4 = String.valueOf(new BigDecimal(buyCount4).setScale(1, RoundingMode.UP)) + "万";

                marketDetailBean.mBuyPrice5 = String.valueOf(new BigDecimal(contents[19]).setScale(2, RoundingMode.UP));
                Double buyCount5 = Double.valueOf(contents[18]) / 10000d;
                marketDetailBean.mBuyCount5 = String.valueOf(new BigDecimal(buyCount5).setScale(1, RoundingMode.UP)) + "万";

                marketDetailBean.mSellPrice1 = String.valueOf(new BigDecimal(contents[21]).setScale(2, RoundingMode.UP));
                Double sellCount1 = Double.valueOf(contents[20]) / 10000d;
                marketDetailBean.mSellCount1 = String.valueOf(new BigDecimal(sellCount1).setScale(1, RoundingMode.UP)) + "万";

                marketDetailBean.mSellPrice2 = String.valueOf(new BigDecimal(contents[23]).setScale(2, RoundingMode.UP));
                Double sellCount2 = Double.valueOf(contents[22]) / 10000d;
                marketDetailBean.mSellCount2 = String.valueOf(new BigDecimal(sellCount2).setScale(1, RoundingMode.UP)) + "万";

                marketDetailBean.mSellPrice3 = String.valueOf(new BigDecimal(contents[25]).setScale(2, RoundingMode.UP));
                Double sellCount3 = Double.valueOf(contents[24]) / 10000d;
                marketDetailBean.mSellCount3 = String.valueOf(new BigDecimal(sellCount3).setScale(1, RoundingMode.UP)) + "万";

                marketDetailBean.mSellPrice4 = String.valueOf(new BigDecimal(contents[27]).setScale(2, RoundingMode.UP));
                Double sellCount4 = Double.valueOf(contents[26]) / 10000d;
                marketDetailBean.mSellCount4 = String.valueOf(new BigDecimal(sellCount4).setScale(1, RoundingMode.UP)) + "万";

                marketDetailBean.mSellPrice5 = String.valueOf(new BigDecimal(contents[29]).setScale(2, RoundingMode.UP));
                Double sellCount5 = Double.valueOf(contents[28]) / 10000d;
                marketDetailBean.mSellCount5 = String.valueOf(new BigDecimal(sellCount5).setScale(1, RoundingMode.UP)) + "万";
                return marketDetailBean;
            }
        }
        return null;
    }

    /**
     * 解析指数
     */
    private MarketIndexBean getIndexInfo(String s) {
        String[] split = s.split("=");
        if (split.length == 2) {
            MarketIndexBean marketIndexBean = new MarketIndexBean();
            marketIndexBean.mSymbol = split[0].substring(split[0].length() - 6, split[0].length());
            String[] contents = split[1].substring(split[1].indexOf("\"") + 1, split[1].lastIndexOf("\"")).split(",");
            if (contents.length == 6) {
                marketIndexBean.mName = contents[0];
                String[] lastPoint = contents[1].split("\\.");
                if (lastPoint.length == 2) {
                    marketIndexBean.mLastPoint = String.valueOf(lastPoint[0]) + "." + String.valueOf(lastPoint[1].substring(0, 2));
                }
                String[] change = contents[2].split("\\.");
                if (change.length == 2) {
                    marketIndexBean.mChange = String.valueOf(change[0]) + "." + String.valueOf(change[1].substring(0, 2));
                }
                marketIndexBean.mChangePercent = contents[3] + "%";
                Double volume = Double.valueOf(contents[4]) / 1000000;
                marketIndexBean.mVolume = String.valueOf(new BigDecimal(volume).setScale(2, RoundingMode.UP)) + "亿";
                Double turOver = Double.valueOf(contents[5]) / 10000;
                marketIndexBean.mTurOver = String.valueOf(new BigDecimal(turOver).setScale(2, RoundingMode.UP)) + "亿";
            }
            return marketIndexBean;
        } else {
            return null;
        }
    }

}
